/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.service.content.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.constant.ContentStatic;
import org.grayrabbit.cms.constant.DictionaryStaticValues;
import org.grayrabbit.cms.dao.content.ContentAttachDao;
import org.grayrabbit.cms.dao.content.VideoContentDao;
import org.grayrabbit.cms.entity.Admin;
import org.grayrabbit.cms.entity.ContentAttach;
import org.grayrabbit.cms.entity.OperLog;
import org.grayrabbit.cms.entity.VideoContent;
import org.grayrabbit.cms.service.content.ContentService;
import org.grayrabbit.cms.service.content.VideoContentService;
import org.grayrabbit.cms.vo.content.ContentAttachBean;
import org.grayrabbit.cms.vo.content.ContentBean;
import org.grayrabbit.cms.vo.content.VideoContentBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.grayrabbit.dao.util.Page;
import org.grayrabbit.service.impl.GenericServiceImpl;
/**
 * <b>OperLog</b>的service实现类，事务加此处：

 * @version 1.0,创建时间: 2011-05-16 14:40:11  
 */
@Service
@Transactional
public class VideoContentServiceImpl extends GenericServiceImpl implements VideoContentService {
	/**
	 * 注入dao层：
	 */
	@Autowired
	@Qualifier("videoContentDaoImpl")
	private VideoContentDao videoContentDao;
	
	@Autowired
	@Qualifier("contentAttachDaoImpl")
	private ContentAttachDao contentAttachDao;
	
	/**
	 * 此方法执行时为只读事务：
	 * @see ContentService#getOperLog(String)
	 */
	@Transactional(readOnly=true)
	public VideoContent getVideoContent(final String videoContentId){
		VideoContent videoContent = videoContentDao.findById(videoContentId);
		videoContentDao.initialize(videoContent);
		videoContentDao.initialize(videoContent.getCategory());
		videoContentDao.initialize(videoContent.getContentAttachs());
		videoContentDao.initialize(videoContent.getContentTopic());
		videoContentDao.initialize(videoContent.getTopic());
		return videoContent;
	}
	
	/**
	 * @see ContentService#delete(String)
	 */
	@Transactional
	public void delete(final VideoContent videoContent) {
		videoContentDao.makeTransient(videoContent);
	}
	
	/**
	 * @see ContentService#save(OperLog)
	 */
	@Transactional
	public void save(final VideoContent videoContent, final Admin user,final String cflag) {
		if(videoContent!=null){
			if(StringUtils.isBlank(videoContent.getContentId())){
				if(StringUtils.isNotBlank(user.getLoginName())){
					videoContent.setCreateLoginName(user.getLoginName());
					videoContent.setUpdateLoginName(user.getLoginName());
				}
				if(StringUtils.isNotBlank(user.getRealName())){
					videoContent.setCreateRealName(user.getRealName());
					videoContent.setUpdateRealName(user.getRealName());
				}else{
					videoContent.setCreateRealName(user.getLoginName());
					videoContent.setUpdateRealName(user.getLoginName());
				}
				videoContent.setCreateTime(new Date());
				videoContent.setUpdateTime(new Date());
				videoContent.setContentStatus(DictionaryStaticValues.CONTENT_STATUS_NEW);
				videoContent.setIsLocked(new Boolean(false));
			}else if(DictionaryStaticValues.CONTENT_STATUS_SUCCESS.equals(cflag)
					||DictionaryStaticValues.CONTENT_STATUS_FAIL.equals(cflag)){
				videoContent.setAuditLoginName(user.getLoginName());
				videoContent.setAuditRealName(user.getRealName());
				videoContent.setAuditTime(new Date());
				videoContent.setUpdateLoginName(user.getLoginName());
				videoContent.setUpdateRealName(user.getRealName());
				videoContent.setUpdateTime(new Date()); 
				if(videoContent.getPublishTime()==null){
					videoContent.setPublishTime(new Date());
				}
				videoContent.setContentStatus(cflag);
				videoContent.setIsLocked(new Boolean(false));
			}else if(DictionaryStaticValues.CONTENT_STATUS_PUBLISH.equals(cflag)){
				videoContent.setPublishLoginName(user.getLoginName());
				videoContent.setPublishRealName(user.getRealName());
				if(videoContent.getPublishTime()==null){
					videoContent.setPublishTime(new Date());
				}
				videoContent.setContentStatus(cflag);
				videoContent.setIsLocked(new Boolean(false));
			}else if(DictionaryStaticValues.CONTENT_STATUS_LOCK.equals(cflag)){
				/*锁定 不记录修改xinxi	   */
				videoContent.setIsLocked(new Boolean(true));
			}else{
				if(cflag.equals(DictionaryStaticValues.CONTENT_STATUS_NEW)
						||cflag.equals(DictionaryStaticValues.CONTENT_STATUS_PUBLISH)){
					if(videoContent.getTemporaryUser()!=null){
						videoContent.setUpdateRealName(videoContent.getTemporaryUser());
					}else{
						videoContent.setUpdateRealName(user.getRealName());
					}
				}else{
					
					videoContent.setUpdateRealName(user.getRealName());
					videoContent.setTemporaryUser(user.getRealName());
				}
				videoContent.setUpdateLoginName(user.getLoginName());
				videoContent.setUpdateRealName(user.getRealName());
				videoContent.setUpdateTime(new Date()); 
				videoContent.setContentStatus(DictionaryStaticValues.CONTENT_STATUS_NEW);
				videoContent.setIsLocked(new Boolean(false));
			}
		}
		videoContentDao.makePersistent(videoContent);		
	}
	
	/**
	 * 此方法执行时为只读事务：
	 * @see ContentService#getPage(ContentBean)
	 */
	@Transactional(readOnly=true)
	public Page<VideoContent> getPage(final VideoContentBean videoContentBean){
		Page<VideoContent> page =  new Page<VideoContent>();
		page.setTotal(videoContentDao.count(videoContentBean));
		page.setResult(videoContentDao.findByQc(videoContentDao.assembly(videoContentBean)));
		return page;
	}

	/**
	 * 此方法执行时为只读事务：
	 * @see ContentService#list(Page, ContentBean)
	 */
	public void list(Page<VideoContent> page, VideoContentBean videoContentBean) {
		if(page==null)
			return;
		if(page.isAutoCount()){
			page.setTotal(videoContentDao.count(videoContentBean));
		}
		page.setResult(videoContentDao.findByPage(page,videoContentBean));
	}
	
	/**
	 * 根据主键id列删除，批量删除OperLog
	 * @see ContentService#delete(String[])
	 */
	public void delete(final String[] key) {
		int i =0;
		for(String id : key){
			ContentAttachBean obj = new ContentAttachBean();
			obj.setContent(videoContentDao.findById(id));
			List<ContentAttach> list= contentAttachDao.findContentAttch(obj);
			for(int j = 0 ; j < list.size();j++){
				contentAttachDao.makeTransient(list.get(j));
				if(j==MAX_CLEAR)
					contentAttachDao.clear();
			}
			videoContentDao.makeTransient(videoContentDao.findById(id));
			i++;
			if(i==MAX_CLEAR)
				videoContentDao.clear();
			if(ContentStatic.cab!=null){
				ContentStatic.cab.remove(id);
			}
		}
		
	}
	
}